package com.woan.api.idao.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.woan.api.idao.ZbConfigIDAO;
import com.woan.api.idao.ZbControlIDAO;
import com.woan.api.model.SysVideo;
import com.woan.api.model.ZbConfig;
import com.woan.api.model.ZbControl;
import com.woan.api.vo.VCustomvideoList;
import com.woan.api.vo.VvideoList;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZbControl entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.woan.api.model.ZbControl
 * @author MyEclipse Persistence Tools
 */
public class ZbConfigDAO extends HibernateDaoSupport implements ZbConfigIDAO{
	//保存视频配置
	public boolean SaveVideoConfig(ZbConfig zbconfig)
	{
//		try {
//			getHibernateTemplate().save(zbconfig);
//			return true;
//		} catch (RuntimeException re) {
//			throw re;
//		}
		Session session = this.getSession();
		session.save(zbconfig);
		session.flush();
		session.clear();
		return true;
	}
	//删除视频配置
	public boolean DeleteVideoConfig(String groupid)
	{
		Session session = this.getSession();
		session.createSQLQuery("delete from zb_config where groupId='"+ groupid+"' and device='ipad'").executeUpdate();
		return true;
	}
	//获取配置
	public List<ZbConfig> FindListConfig(String groupid)
	{
		StringBuffer buffer = new StringBuffer(
				"select * from zb_config where groupId='"+groupid+"' and device = 'ipad' order by position ");
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(ZbConfig.class));
			List<ZbConfig> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public List<VCustomvideoList> FindConfigLiveVideos(String groupid)
	{
		Session session = this.getSession();
		StringBuffer buffer=new StringBuffer(" SELECT vid,picture,userName as \"userName\",address,detail, ");
		buffer.append(" videoSize as \"videoSize\",videoLength as \"videoLength\",videoPath as \"videoPath\",beginTime as  \"beginTime\" , ");
		buffer.append(" endTime as \"endTime\",title, ");
		buffer.append(" longitude,latitude,countrycode,countryname,province, ");
		buffer.append(" city,counties,position FROM ( ");
		
		buffer.append(" select a.userName as userName ,a.position AS position,b.vid AS vid,a.groupId AS groupId, ");
		buffer.append(" b.picture AS picture,b.address AS address, ");
		buffer.append(" b.detail AS detail,b.videoSize AS videoSize,b.videoLength AS videoLength, ");
		buffer.append(" b.videoPath AS videoPath,b.beginTime AS beginTime,b.endTime AS endTime,b.title AS title, ");
		buffer.append(" b.longitude AS longitude,b.latitude AS latitude,b.countrycode AS countrycode,b.countryname AS countryname, ");
		buffer.append(" b.province AS province,b.city AS city,b.counties AS counties ");
		buffer.append(" from (zb_config a left join (select sb.vid,sb.uid,sb.userName,sb.rid,sb.createTime,sb.title,sb.detail,sb.address,sb.longitude,sb.latitude,sb.countrycode,sb.countryname,sb.province,sb.city,sb.counties, v.picture,v.status,v.videoSize,v.videoLength,v.videoPath,v.beginTime,v.endTime,v.isdel from sys_video_basic sb,sys_video v WHERE v.status=0 and sb.rid='"+groupid+"' and sb.vid=v.vid and v.videotype='ios') b ");
		buffer.append(" on((a.userName = b.userName)) ) where   a.device='ipad' ");
		buffer.append(" ) c where groupid='"+groupid+"' order by position");
		System.out.println(buffer.toString());
		
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VCustomvideoList.class));
			//Query query =session.createSQLQuery(buffer.toString()).addEntity(VCustomvideoList.class);
			List<VCustomvideoList> list= new ArrayList<VCustomvideoList>();list=query.list();
			//System.out.println(list.size());
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public String QueryConfigPushOrNot(String groupid,String username)
	{
		Session session = this.getSession();
		StringBuffer buffer = new StringBuffer(
				"select message from zb_config where groupId='"+groupid+"' and device = 'pc' and userName='"+username+"' order by position ");
		System.out.println(buffer.toString());
		Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
				Transformers.aliasToBean(ZbConfig.class));
		List<ZbConfig> list=query.list();
		String message="";
		//System.out.println(list);
		if(list.size()<=0)
		{
			message="";
		}
		else
		{
			ZbConfig zbconfig = (ZbConfig)list.get(0);
			message = zbconfig.getMessage();
	        //System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+message);
		}
		
		return message;
	}
}